package com.sjb.cp.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sjb.cp.annotation.AuthCheck;
import com.sjb.cp.common.BaseResponse;
import com.sjb.cp.common.DeleteRequest;
import com.sjb.cp.common.ResultUtils;
import com.sjb.cp.exception.BusinessException;
import com.sjb.cp.exception.ErrorCode;
import com.sjb.cp.exception.ThrowUtils;
import com.sjb.cp.model.constant.UserConstant;
import com.sjb.cp.model.po.User;
import com.sjb.cp.model.request.user.UserAddRequest;
import com.sjb.cp.model.request.user.UserQueryRequest;
import com.sjb.cp.model.request.user.UserRegisterRequest;
import com.sjb.cp.model.request.user.UserUpdateRequest;
import com.sjb.cp.model.vo.UserLoginVo;
import com.sjb.cp.model.vo.UserVo;
import com.sjb.cp.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author:sjb
 * @CreateTime:2024-12-10
 * @Description: 用户视图层
 * @Version：1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;


    @PostMapping("/register")
    public BaseResponse<Long> register(@RequestBody UserRegisterRequest userRegisterRequest){
        ThrowUtils.throwIf(userRegisterRequest == null, ErrorCode.PARAMS_ERROR);
        String userAccount = userRegisterRequest.getUserAccount();
        String password = userRegisterRequest.getPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        String userType = userRegisterRequest.getUserType();

        long userRegister = userService.userRegister(userAccount, password, checkPassword,userType);
        return ResultUtils.success(userRegister);
    }

    @PostMapping("/login")
    public BaseResponse<UserLoginVo> login(String userAccount, String password, HttpServletRequest request){
        ThrowUtils.throwIf(userAccount == null, ErrorCode.PARAMS_ERROR);

        UserLoginVo userLoginVo = userService.userLogin(userAccount, password, request);
        return ResultUtils.success(userLoginVo);
    }

    @GetMapping("/getCurrentUser")
    public UserLoginVo getLoginUser(HttpServletRequest request){
        User loginUser = userService.getLoginUser(request);
        return userService.getLoginUserVo(loginUser);
    }

    @PostMapping("/logOut")
    public BaseResponse<Boolean> louOut(HttpServletRequest request){
        boolean logOut = userService.logOut(request);
        return ResultUtils.success(logOut);
    }

    @PostMapping("/addUser")
    @AuthCheck(mustRole = UserConstant.MUST_ADMIN)
    public BaseResponse<Long> addUser(@RequestBody UserAddRequest userAddRequest){
        ThrowUtils.throwIf(userAddRequest == null, ErrorCode.PARAMS_ERROR);

        User user = new User();
        BeanUtils.copyProperties(userAddRequest, user);

        String password = "could-picture";
        String encryptPassword = userService.getEncryptPassword(password);
        user.setUserPassword(encryptPassword);

        boolean saved = userService.save(user);
        if (!saved){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"添加用户失败");
        }
        return ResultUtils.success(user.getId());
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateRequest userUpdateRequest){
        ThrowUtils.throwIf(userUpdateRequest == null,ErrorCode.PARAMS_ERROR);

        User user = new User();
        BeanUtils.copyProperties(userUpdateRequest,user);

        boolean updated = userService.updateById(user);
        if (!updated){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"修改用户信息失败");
        }
        return ResultUtils.success(true);
    }

    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.MUST_ADMIN)
    public BaseResponse<Boolean> deleteUser(@RequestBody DeleteRequest deleteRequest){
        ThrowUtils.throwIf(deleteRequest == null,ErrorCode.PARAMS_ERROR);

        boolean removedById = userService.removeById(deleteRequest.getId());
        return ResultUtils.success(removedById);

    }

    @PostMapping("/get/page/vo")
    @AuthCheck(mustRole = UserConstant.MUST_ADMIN)
    public BaseResponse<Page<UserVo>> getUserList(@RequestBody UserQueryRequest userQueryRequest){
        ThrowUtils.throwIf(userQueryRequest == null, ErrorCode.PARAMS_ERROR);

        long current = userQueryRequest.getCurrent();
        long pageSize = userQueryRequest.getPageSize();

        Page<User> userPage = userService.page(new Page<>(current, pageSize),
                userService.getQueryWrapper(userQueryRequest));

        Page<UserVo> userVoPage = new Page<>(current, pageSize, userPage.getTotal());
        List<UserVo> userVOList = userService.getUserVoList(userPage.getRecords());
        userVoPage.setRecords(userVOList);
        return ResultUtils.success(userVoPage);
    }
}
